﻿@model  RBAC.Web.Models.RoleModel
@using RBAC.Web.CustomHelpers
@using RBAC.Web.Mapper
@using RBAC.Web.Utilities
@{
    ViewBag.Title = "Edit Role";

    string returnUrl = Url.Action("ListRole", "Role");
}
@section Styles {
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/form.js?3")" type="text/javascript"></script>
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" />
    <link href="@Url.Content("~/Content/themes/base/jquery-ui.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.datepicker.css")" rel="stylesheet" type="text/css" />

    <link href="@Url.Content("~/Content/bootstrap/css/utility.css")" rel="stylesheet"		type="text/css">

    <script type="text/javascript">
        $(function () {
            //How to setup Datepicker
            SetDatePicker("CreateDate", new Date(), '-71Y', 'c-70:c');
            SetDatePicker("FreeDate");
            
            //How to setup Lookup
			
            //Master Detail
			 
							 
							 
							 
							 
							 
							 
				 
            SetOneToManyFor("Permission", '@Url.Action("DeletePermissions", "Permission")', '@Url.Action("EditRole", "Role", new { RoleCode = Model.RoleCode })', "form-Role");
        });
    </script>
}


<div class="row">
    <div class="col-lg-12">
        <h2 class="page-header">Role</h2>
    </div>
</div>

@using (Html.BeginForm("Update", "Role", FormMethod.Post, new { @id = "form-Role", @class = "form-horizontal", role = "form" }))
{


    //version 1.4 Bootstrap 7/4/2557
    <div class="panel panel-primary">
        <div class="panel-heading">Role Model</div>
        <div class="panel-body">





            @Html.HiddenFor(model => model.Id)

            <div class="form-group row">



                <div class="col-md-6">
                    <div class="col-md-4">
                        <label class="word-control-res">@Html.LabelFor(model => model.RoleCode)</label>
                    </div>
                    <div class="col-md-8">
                        @Html.TextBoxFor(model => model.RoleCode, new { @class = "form-control" })

                    </div>
                    <div class="col-md-offset-4 col-md-8">
                        @Html.ValidationMessageFor(model => model.RoleCode)
                    </div>
                </div>
                @Html.HiddenFor(model => model.Id)
                <input type="hidden" name="Mode" value="Edit" />
                <input type="hidden" name="OldValue" value="@(Model.RoleCode == null ? "" : Model.RoleCode)" />



                <div class="col-md-6">
                    <div class="col-md-4">
                        <label class="word-control-res">@Html.LabelFor(model => model.RoleName)</label>
                    </div>
                    <div class="col-md-8">
                        @Html.TextBoxFor(model => model.RoleName, new { @class = "form-control" })

                    </div>
                    <div class="col-md-offset-4 col-md-8">
                        @Html.ValidationMessageFor(model => model.RoleName)
                    </div>
                </div>



                <div class="col-md-6">
                    <div class="col-md-4">
                        <label class="word-control-res">@Html.LabelFor(model => model.Description)</label>
                    </div>
                    <div class="col-md-8">
                        @Html.TextBoxFor(model => model.Description, new { @class = "form-control" })

                    </div>
                    <div class="col-md-offset-4 col-md-8">
                        @Html.ValidationMessageFor(model => model.Description)
                    </div>
                </div>




                <div class="col-md-6">
                    <div class="col-md-4">
                        <label class="word-control-res">@Html.LabelFor(model => model.CreateDate)</label>
                    </div>
                    <div class="col-md-8 ">
                        @Html.DatePickerFor(model => model.CreateDate, "CreateDate", true)

                    </div>
                    <div class="col-md-offset-4 col-md-8">
                        @Html.ValidationMessageFor(model => model.CreateDate)
                    </div>
                </div>





                <div class="col-md-6">
                    <div class="col-md-4">
                        <label class="word-control-res">@Html.LabelFor(model => model.RoleStatus)</label>
                    </div>
                    <div class="col-md-8">
                        @Html.RadioButtonForSelectList(model => model.RoleStatus, RoleStatusMapper.GetRoleStatusSelectList())

                    </div>
                    <div class="col-md-offset-4 col-md-8">
                        @Html.ValidationMessageFor(model => model.RoleStatus)
                    </div>
                </div>



            </div>

            <div class="col-md-6">
                <div class="btn-group">
                    <label>
                        <input type="button" name="btnBack" value="Cancel" class="btn btn-primary" id="button-back" onclick="location.href='@returnUrl    '"/>
                    </label>
                    <label>
                        <input type="Button" value="Save" id="button-save" class="btn btn-primary" onclick="JavaScript: SaveButtonClick()" />
                    </label>
                    <label>
                        <input type="button" name="btnDelete" value="Delete" class="btn btn-primary" id="button-delete" onclick="JavaScript: DeleteButtonClick()" />
                    </label>
                </div>
            </div>




        </div>
    </div> 

    // Master Detail Dest Class

    bool highlightPermission = false;
    if (ViewData["PermissionId"] != null)
    {
        try
        {
            int index = Model.Permissions.IndexOf(Model.Permissions.FirstOrDefault(m => m.Id == Convert.ToInt32(ViewData["PermissionId"])));
            if (index > -1)
            {
                var item = Model.Permissions.ElementAt(index);
                Model.Permissions.RemoveAt(index);
                Model.Permissions.Insert(0, item);

                highlightPermission = true;
            }
        }
        catch { }
    }
    var Permissiongrid = new WebGrid(source: Model.Permissions, canPage: false);
    if (highlightPermission)
    {
        Permissiongrid.SelectedIndex = 0;
    }
    <input type="hidden" id="Permission_hid_delete" value="Delete" name="DeleteButton" /> 
   
    @Html.Hidden("Role_Id", Model.Id)
    
 


    <div>
        @Html.DisplayMessage(Convert.ToString(ViewData["PermissionMessage"]), ViewData["PermissionStatus"] as bool?)
    </div>

    <div class="modal" id="Permission-dialog-alert">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title">Delete SaleOrder</h4>
                </div>
                <div class="modal-body">
                    No Permission was selected.
                </div>
                <div class="modal-footer">
                    <a href="#" data-dismiss="modal" class="btn btn-primary">Ok</a>

                </div>
            </div>
        </div>
    </div>

  
    <div class="modal" id="Permission-confirm-delete">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title">Delete SaleOrder</h4>
                </div>
                <div class="modal-body">
                    Do you want to delete?
                </div>
                <div class="modal-footer">
                    <button type="button" data-dismiss="modal" id="Permission-delete-data" class="btn btn-primary">YES</button>
                    <button type="button" data-dismiss="modal" class="btn btn-primary">NO</button>
                </div>
            </div>
        </div>
    </div>

	
		
    <div class="panel panel-primary">
        <div class="panel-heading">Permissions</div>
        <div class="panel-body">


            <div class="col-md-6">
                <div class="btn-group">
                    <label>
                        <input class="btn btn-primary" id="Permission-button-add" type="button" value="New" onclick="location.href='@Url.Action("Add", "Permission", new { Role_id = Model.Id, RoleCode = Model.RoleCode })    '"/></label>
                    <label>
                        <input class="btn btn-primary" type="button" value="Delete" id="Permission-button-delete" /></label>
                </div>
            </div>


            @if (Model.Permissions != null && Model.Permissions.Count > 0)
            {    
                <div style="overflow: scroll; width: 100%; height: auto; max-height: 400px;">
                    @Permissiongrid.GetHtml(
        htmlAttributes: new { id = "Permission" },
        tableStyle: "table table-striped table-bordered table-hover dataTable no-footer grid",
        rowStyle: "row-a",
        alternatingRowStyle: "row-b",
        selectedRowStyle: "selected-row",
        columns: Permissiongrid.Columns(
            Permissiongrid.Column(header: "", style: "width_0per", format: @<text><input name="PermissionCheckbox" type="checkbox" value="@item.Id" /></text>),
            Permissiongrid.Column(header: "#", style: "width_0per", format: item => item.WebGrid.Rows.IndexOf(item) + 1)
        //TODO : สามารถ Edit ได้ --
        // ทดสอบ Edit  
        					
			
			
												
							
						@* ถ้า attribute ของลูก เป็น LinkEdit *@
            , Permissiongrid.Column(header: "Permission", style: "width_0per", columnName: "PermissionName",
            format: item =>
            {
                string link = "<a href=\"{0}?PermissionName={2}\" class=\"word_wrap\">{1}</a>";
                return Html.Raw(string.Format(link, @Url.Content("~/Permission/EditPermission"), item.Value.PermissionName, item.Value.PermissionName));
            })
			
			
												
							
			
			
			           @* ถ้า attribute ของลูก ไม่เป็น LinkEdit , DataType เป็น String *@
, Permissiongrid.Column(header: "Description", canSort: false, style: "width_100per", columnName: "Description",
                format: item =>
                {
                    string Description = "<div class=\"word_wrap\">{0}</div>";
                    return Html.Raw(string.Format(Description, String.IsNullOrWhiteSpace(item.Description) ? "" : item.Value.Description));
                })   
			

			
						
							
			
			
			            @* ถ้า attribute ของลูก ไม่เป็น LinkEdit , DataType เป็น DateTime *@
, Permissiongrid.Column(header: "Create Date", style: "width_0per", columnName: "Create Date",
                format: item =>
                {
                    string CreateDate = "<div class=\"word_wrap\">{0}</div>";
                    return Html.Raw(string.Format(CreateDate, DateTime.Parse(item.Value.CreateDate.ToString()).ToString("dd/MM/yyyy", new System.Globalization.CultureInfo("th-TH"))));
                }
            )
			

			
						
							
			
			
						@* START ดึง External Lookup Field *@
			 
			

						
							
			
			
						@* START ดึง External Lookup Field *@
			 
			

						
							
			
			
												
							
			
			
						@* START ดึง External Lookup Field *@
									@* ถ้า Lookup attribute เป็น String *@
                    
        //                    chayawooth****

//, Permissiongrid.Column(header: "RoleLookupRole Code", canSort: false, style: "width_100per", columnName: "RoleLookupRole Code",
        //                format: item =>
        //                {
        //                    string RoleCode = "<div class=\"word_wrap\">{0}</div>";
        //                    return Html.Raw(string.Format(RoleCode, String.IsNullOrWhiteSpace(item.RoleLookupRoleCode) ? "" : item.Value.RoleLookupRoleCode));
        //                })  


//                 chayawooth****
						@* END ดึง External Lookup Field *@
			 
			

						
		             
))
                </div>
            }
            else
            {
                <div class="emptyRow">No Permission for this Role.</div>
            }

        </div>
    </div>

}




<div class="modal" id="Delete">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">Delete Role</h4>
            </div>
            <div class="modal-body">
                Do you want to delete?
            </div>
            <div class="modal-footer">
                <button type="button" data-dismiss="modal" id="confirm-delete" class="btn btn-primary">YES</button>
                <button type="button" data-dismiss="modal" class="btn btn-primary">NO</button>
            </div>
        </div>
    </div>
</div>


<div class="modal" id="Save">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">Save Role</h4>
            </div>
            <div class="modal-body">
                Do you want to save?
            </div>
            <div class="modal-footer">
                <button type="button" data-dismiss="modal" id="confirm-save" class="btn btn-primary">YES</button>
                <button type="button" data-dismiss="modal" class="btn btn-primary">NO</button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(function () {

        $("#button-delete").click(function () {
              
          
            $('#Delete').modal('show');
            $('#confirm-delete').click(function () {
                $.post('@Url.Action("DeleteRoleById", "Role")',$("#form-Role").serialize(),function (data) {
                    var s = '@returnUrl'
                    if(s.indexOf("?") !== -1)
                        window.location.href = s + '&result=' + data + '&operation=delete';   
                    else
                        window.location.href = s + '?result=' + data + '&operation=delete';
                });
                         
                   
            });
        });
    
 

        $("#button-save").click(function () {
            
            $('#Save').modal('show'); 
            $('#confirm-save').click(function () {
                $("#form-Role").submit();
            });
        });
    });
</script>
